#include"../../inc/net/TaskQueue.h"
#include"../../inc/net/Mutexlock.h"
#include"../../inc/net/Condition.h"

namespace SC
{
    bool TaskQueue::isEmpty()
    {
        return _queue.empty();
    }

    bool TaskQueue::isFull()
    {
        return _queue.size() == _queuesize;
    }

    void TaskQueue::push(Task task)
    {
        //
        MutexGuard lock(_mutex);
        while(isFull())
        {
            _cond.wait();
        }

        _queue.push(task);

        _cond.wake();
    }

    Task TaskQueue::pop()
    {
        MutexGuard lock(_mutex);
        while (!_ExitSig && isEmpty())
        {
            _cond.wait();
        }

        if(_ExitSig)
            return nullptr;
        else
        {
            Task task = _queue.front();
            _queue.pop();

            _cond.wake();
            return task;
        }
    }

    void TaskQueue::exit()
    {
        if(!_ExitSig)
        {
            _ExitSig = true;
        }
        _cond.wakeall();
    }
}